package cn.net.ecode.elec.api.web;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.cms.entity.Article;
import cn.net.ecode.modules.cms.entity.Category;
import cn.net.ecode.modules.cms.entity.Site;
import cn.net.ecode.modules.cms.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName AppUserController
 * @Description 用户注册接口
 * @Author AlanShelby
 * @Date 2019/8/27 0027 8:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/cms")
public class AppCmsController extends BaseController {
    @Autowired
    private ArticleService articleService;

    /**
     * 获取cms page
     *
     * @param siteCode
     * @param categoryCode
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "articleList", method = RequestMethod.GET)
    public Page<Article> articleList(@RequestParam String siteCode, @RequestParam String categoryCode, @RequestParam int pageNo, @RequestParam int pageSize) {
        if (!StringUtils.isBlank(siteCode) && !StringUtils.isBlank(categoryCode)) {
            Category category = new Category();
            category.setSite(new Site(siteCode));
            Page<Article> page = new Page();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);

            if (!"0".equals(categoryCode)) {
                category.setCategoryCode(categoryCode);
                category.setParentCodes(categoryCode);
            }
            Article article = new Article(category);
            article.setStatus("0");
            article.setCategoryCode(categoryCode);
            page = articleService.findPage(page, article, false);
            return page;
        } else {
            return new Page<>();
        }
    }

    /**
     * 获取内容详情
     *
     * @param id 内容id
     * @return
     */
    @RequestMapping(value = "getArticle", method = RequestMethod.GET)
    public Article getArticle(@RequestParam String id) {
        if (StringUtils.isNotBlank(id)) {
            Article article = new Article();
            article.setId(id);
            article = articleService.get(article);
            if(article!=null){
                String content="";

                content=article.getArticleData().getContent().replaceAll("src=\"","src=\""+ Global.getConfig("cms-content-url"));
                article.getArticleData().setContent(content);
            }
            return article;
        } else {
            return new Article();
        }
    }
}
